package com.txunda.zhuohired.adapter.user.my;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.bean.user.my.BankListBean;

import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/8 14:20
 * 功能描述：
 * 联系方式：jiahuanchen@126.com
 */

public class MyBankAdapter extends BaseAdapter {
    private Context context;
    private ViewHolder holder;
    private List<BankListBean.DataBean> bankList;
    /**
     * 自定义监听
     */
    private HolderClickListener mHolderClickListener;

    public MyBankAdapter(Context context, List<BankListBean.DataBean> bankList) {
        this.context = context;
        this.bankList = bankList;
    }

    @Override
    public int getCount() {
        return bankList.size();
    }

    @Override
    public Object getItem(int i) {
        return bankList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_my_bank, null);
            holder = new ViewHolder();
            view.setTag(holder);
            ViewUtils.inject(holder, view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        Glide.with(context).load(bankList.get(i).getBank_icon()).error(R.mipmap.icon_defult).placeholder(R.mipmap
                .icon_defult).dontAnimate().into(holder.iv_item_my_bank);
        if (TextUtils.isEmpty(bankList.get(i).getBank_name())) {
            holder.tv_my_bank_name.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_my_bank_name.setVisibility(View.VISIBLE);
            holder.tv_my_bank_name.setText(bankList.get(i).getBank_name());
        }
        if (TextUtils.isEmpty(bankList.get(i).getCard_number())) {
            holder.tv_item_my_bank_num.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_item_my_bank_num.setVisibility(View.VISIBLE);
            holder.tv_item_my_bank_num.setText("**** **** **** **** " + bankList.get(i).getCard_number().substring
                    (bankList.get(i).getCard_number().length() - 4, bankList.get(i).getCard_number().length()));
        }
        holder.te_item_my_bank_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHolderClickListener.onSureHolderClick(i);
            }
        });
        return view;
    }

    class ViewHolder {
        /**
         * 取消关联
         */
        @ViewInject(R.id.te_item_my_bank_cancel)
        private TextView te_item_my_bank_cancel;
        /**
         * 银行图片
         */
        @ViewInject(R.id.iv_item_my_bank)
        private ImageView iv_item_my_bank;
        /**
         * 银行卡名称
         */
        @ViewInject(R.id.tv_my_bank_name)
        private TextView tv_my_bank_name;
        /**
         * 银行卡号
         */
        @ViewInject(R.id.tv_item_my_bank_num)
        private TextView tv_item_my_bank_num;
    }
    /**
     * 自定义监听
     *
     * @param holderClickListener
     */
    public void SetOnClickListener(HolderClickListener holderClickListener) {
        this.mHolderClickListener = holderClickListener;
    }

    /**
     * 自定义监听
     */
    public interface HolderClickListener {
        void onSureHolderClick(int position);

    }
}
